package com.ths.inputservice.ui;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListPopupWindow;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.ths.inputservice.R;
import com.ths.inputservice.adapter.RtspHistoryAdapter;
import com.ths.inputservice.utils.RtspHistoryManager;

public class SettingsActivity extends AppCompatActivity {

    private EditText et1;
    private EditText et2;
    private ListPopupWindow popupWindow1;
    private ListPopupWindow popupWindow2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        et1 = findViewById(R.id.et_rtsp1);
        et2 = findViewById(R.id.et_rtsp2);
        ImageButton btnDropdown1 = findViewById(R.id.btn_dropdown1);
        ImageButton btnDropdown2 = findViewById(R.id.btn_dropdown2);
        Button save = findViewById(R.id.btn_save);
        Button cancel = findViewById(R.id.btn_cancel);

        SharedPreferences sp = getSharedPreferences("rtsp", Context.MODE_PRIVATE);
        et1.setText(sp.getString("url1", "rtsp://192.168.144.25:8554/main.264"));
        et2.setText(sp.getString("url2", "rtsp://192.168.144.26:8554/main.264"));

        // 设置下拉按钮点击事件
        btnDropdown1.setOnClickListener(v -> showHistoryPopup(1, et1, v));
        btnDropdown2.setOnClickListener(v -> showHistoryPopup(2, et2, v));

        save.setOnClickListener(v -> {
            String url1 = et1.getText().toString().trim();
            String url2 = et2.getText().toString().trim();
            
            sp.edit().putString("url1", url1)
                    .putString("url2", url2)
                    .apply();
                    
            // 保存到历史记录
            if (!url1.isEmpty()) {
                RtspHistoryManager.addToHistory(this, 1, url1);
            }
            if (!url2.isEmpty()) {
                RtspHistoryManager.addToHistory(this, 2, url2);
            }
            
            finish();
        });
        cancel.setOnClickListener(v -> finish());
    }
    
    private void showHistoryPopup(int channelId, EditText targetEditText, View anchor) {
        // 获取历史记录
        java.util.List<String> historyList = RtspHistoryManager.getHistory(this, channelId);
        
        // 创建适配器
        RtspHistoryAdapter adapter = new RtspHistoryAdapter(this, historyList);
        
        // 创建并配置下拉窗口
        ListPopupWindow popupWindow = new ListPopupWindow(this);
        popupWindow.setAdapter(adapter);
        popupWindow.setAnchorView(anchor);
        popupWindow.setWidth(targetEditText.getWidth() + anchor.getWidth());
        popupWindow.setModal(true);
        
        // 设置项目点击事件
        popupWindow.setOnItemClickListener((parent, view, position, id) -> {
            if (position < historyList.size()) {
                // 选择了历史项
                targetEditText.setText(historyList.get(position));
            } else {
                // 选择了"关闭"选项
                targetEditText.setText("关闭");
            }
            // 无论选择哪个选项，都关闭弹窗
            popupWindow.dismiss();
        });
        
        // 显示弹窗
        popupWindow.show();
        
        // 保存当前弹窗引用，以便需要时关闭
        if (channelId == 1) {
            if (popupWindow2 != null && popupWindow2.isShowing()) {
                popupWindow2.dismiss();
            }
            popupWindow1 = popupWindow;
        } else {
            if (popupWindow1 != null && popupWindow1.isShowing()) {
                popupWindow1.dismiss();
            }
            popupWindow2 = popupWindow;
        }
    }
}


